home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Random Demo
- ** -----------
- ** Demonstrates the randomness of functions FastRandom() and SlowRandom().
- ** Given the speed of FastRandom I think you will be quite impressed with
- ** it's results. It gets all the numbers and gives a very even balance.
- ** The sequence of numbers is however not the best, so only use it when
- ** speed is critical.
- **
- ** In case you are wondering the FastRandom() routine is just 10 assembler
- ** instructions including the re-seed and range div... Wow!
- **
- ** Compiles under SAS/C.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <proto/games.h>
- #include <proto/exec.h>
-
- struct GMSBase *GMSBase;
- int YesNo;
- UWORD Number;
- ULONG loop, Iterations, UserRange, *NumArray;
-
- /*=========================================================================*/
- /* MAIN CODE */
- /*=========================================================================*/
-
- void main(void)
- {
- struct GamesLibrary *GMSBase = (struct GamesLibrary *)
- OpenLibrary("games.library", 0);
- if (GMSBase == NULL) exit(FALSE);
-
- printf("Random Number Demo");
- printf("\n------------------");
- printf("\nThis program demonstrates the randomness of the games.library functions");
- printf("\nFastRandom() and SlowRandom(). The FastRandom() is just 10 assembler");
- printf("\ninstructions and SlowRandom() is about twice that. Both give very good");
- printf("\nresults, as you will see...\n\n");
-
- do
- {
- printf("Please enter a range between 1 and 500 : ");
- fflush(stdin);
- scanf("%d", &UserRange);
- }
- while (UserRange > 500 || UserRange < 1);
-
- printf("Enter the amount of iterations (Try 500000) : ");
- fflush(stdin);
- scanf("%d", &Iterations);
-
- printf("Do you want to view all the generated numbers (Y/n)? ");
- fflush(stdin);
- if (getchar() == 'n') YesNo = 1;
-
- SetUserPri();
-
- /*=========================================================================*/
- /* GENERATE FAST RANDOM NUMBERS */
- /*=========================================================================*/
-
- NumArray = malloc(sizeof(UWORD));
- for (loop=0; loop<UserRange; loop++)
- NumArray[loop] = 0;
-
- printf("\nPlease wait while generating %d FastRandom() numbers...\n",Iterations);
- do
- {
- Number = FastRandom(UserRange);
- if (YesNo == 0) printf("%d ", Number);
- NumArray[Number]++;
- }
- while (loop++ < Iterations);
-
- printf("\n\nResults are in...\n");
- for (loop=0; loop<UserRange; loop++)
- {
- printf("%4d: %d\n", loop, NumArray[loop]);
- NumArray[loop] = 0;
- };
-
- /*=========================================================================*/
- /* GENERATE SLOW RANDOM NUMBERS */
- /*=========================================================================*/
-
- printf("\nPlease wait while generating %d SlowRandom() numbers...\n",Iterations);
- do
- {
- Number = SlowRandom(UserRange);
- if (YesNo == 0) printf("%d ",Number);
- NumArray[Number]++;
- }
- while (loop++ < Iterations);
-
- printf("\n\nResults are in...\n");
- for (loop=0; loop<UserRange; loop++)
- {
- printf("%4d: %d\n", loop, NumArray[loop]);
- };
-
- CloseLibrary((struct Library *)GMSBase);
- }
-
- /*=========================================================================*/
-
-